//
//  ApplyEnterGroupViewController.m
//  Findif
//
//  Created by Mac on 2019/2/21.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "ApplyEnterGroupViewController.h"
#import "GroupItemsCollectionViewCell.h"

@interface ApplyEnterGroupViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIImageView * topImageView;
@property (nonatomic, strong)UIView * topBackView;
@property (nonatomic, strong)UIView * middleBackView;
@property (nonatomic, strong)UIView * bottomBackView;
@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, weak)UIButton * applyButton;


@end

@implementation ApplyEnterGroupViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UIImageView * topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 150 * SCREENH6 + topHeight)];
    
    topImageView.backgroundColor = [UIColor lightGrayColor];
    topImageView.userInteractionEnabled = YES;
    
    self.topImageView = topImageView;
    
    [self.view addSubview:self.topImageView];
    
    
    UIButton * backButton = [UIButton new];
    
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = backButton;
    
    [self.topImageView addSubview:self.backButton];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.text = @"社团资料";
    titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = titleLabel;
    
    [self.topImageView addSubview:self.titleLabel];
    
    self.topBackView = [self setupBackView];
    [self.view addSubview:self.topBackView];
    self.middleBackView = [self setupBackView];
    [self.view addSubview:self.middleBackView];
    self.bottomBackView = [self setupBackView];
    [self.view addSubview:self.bottomBackView];
    
    UILabel * topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SCREENH6, 8 * SCREENH6, 70 * SCREENH6, 16 * SCREENH6)];
    
    topTitleLabel.text = @"社团介绍";
    topTitleLabel.textColor = ZRBlackColor;
    topTitleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    [self.topBackView addSubview:topTitleLabel];
    
    
    UILabel * topSubLabel = [[UILabel alloc] init];
    
    topSubLabel.text = @"阿斯兰的还不错啦说的不错阿萨德插拔力说的不错拉萨的阿斯兰的还不错啦说的不错阿萨德插拔力说的不错拉萨的";
    topSubLabel.textColor = [UIColor lightGrayColor];
    topSubLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    topSubLabel.numberOfLines = 0;
    
    [self.topBackView addSubview:topSubLabel];
    
    CGSize topSubSize = [self preferredSizeWithMaxWidth:325 * SCREENH6 withLabel:topSubLabel];
    
    topSubLabel.frame = CGRectMake(10 * SCREENH6, 28 * SCREENH6, 325 * SCREENH6, topSubSize.height);
    
    self.topBackView.frame = CGRectMake(15 * SCREENH6, self.topImageView.bottom + 10 * SCREENH6, 345 * SCREENH6, topSubLabel.bottom + 10 * SCREENH6);
    
    
    UILabel * middleTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SCREENH6, 8 * SCREENH6, 70 * SCREENH6, 16 * SCREENH6)];
    
    middleTitleLabel.text = @"社团成员";
    middleTitleLabel.textColor = ZRBlackColor;
    middleTitleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    [self.middleBackView addSubview:middleTitleLabel];
    
    UIImageView * middleRightImageView = [[UIImageView alloc] init];
    
    middleRightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    [self.middleBackView addSubview:middleRightImageView];
    
    [middleRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(middleTitleLabel);
        make.trailing.mas_equalTo(self.middleBackView).offset(-10 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    UILabel * numberLabel = [UILabel new];
    
    numberLabel.text = @"共334人";
    numberLabel.textColor = [UIColor lightGrayColor];
    numberLabel.textAlignment = NSTextAlignmentRight;
    numberLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    [self.middleBackView addSubview:numberLabel];
    
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(middleRightImageView);
        make.trailing.mas_equalTo(middleRightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.itemSize =CGSizeMake(55 * SCREENH6, 80 * SCREENH6);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 15 * SCREENH6);
    //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 28 * SCREENH6, 325 * SCREENH6, 90 * SCREENH6) collectionViewLayout:layout];
    
    collectionView.backgroundColor = ZRWhiteColor;
    collectionView.showsHorizontalScrollIndicator = NO;
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[GroupItemsCollectionViewCell class] forCellWithReuseIdentifier:@"GroupItemsCollectionViewCell"];

    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    //    [collectionView setContentOffset:CGPointMake(-30 * SCREENH6, 0)];
    
    self.collectionView = collectionView;
    
    [self.middleBackView addSubview:self.collectionView];
    
    self.middleBackView.frame = CGRectMake(15 * SCREENH6, self.topBackView.bottom + 10 * SCREENH6, 345 * SCREENH6, self.collectionView.bottom + 5 * SCREENH6);
    
    self.bottomBackView.frame = CGRectMake(15 * SCREENH6, self.middleBackView.bottom + 10 * SCREENH6, 345 * SCREENH6, 50 * SCREENH6);
    
    UILabel * bottomTitleLabel = [[UILabel alloc] init];
    
    bottomTitleLabel.text = @"管理员";
    bottomTitleLabel.textColor = ZRBlackColor;
    bottomTitleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    [self.bottomBackView addSubview:bottomTitleLabel];
    
    [bottomTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomBackView);
        make.leading.mas_equalTo(self.bottomBackView).offset(10 * SCREENH6);
    }];
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.backgroundColor = [UIColor lightGrayColor];
    iconImageView.layer.cornerRadius = 20 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    [self.bottomBackView addSubview:iconImageView];
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomBackView);
        make.leading.mas_equalTo(bottomTitleLabel.mas_trailing).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(40 * SCREENH6);
    }];
    
    UIImageView * bottomRightImageView = [[UIImageView alloc] init];
    
    bottomRightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    [self.bottomBackView addSubview:bottomRightImageView];
    
    [bottomRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bottomBackView);
        make.trailing.mas_equalTo(self.bottomBackView).offset(-10 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    UILabel * bottomNumberLabel = [UILabel new];
    
    bottomNumberLabel.text = @"共334人";
    bottomNumberLabel.textColor = [UIColor lightGrayColor];
    bottomNumberLabel.textAlignment = NSTextAlignmentRight;
    bottomNumberLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    [self.bottomBackView addSubview:bottomNumberLabel];
    
    [bottomNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bottomRightImageView);
        make.trailing.mas_equalTo(bottomRightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    
    UIButton * applyButton = [UIButton new];
    
    [applyButton setBackgroundColor:ZRBlackColor];
    [applyButton setTitle:@"申请入团" forState:UIControlStateNormal];
    [applyButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    applyButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    applyButton.layer.cornerRadius = 5 * SCREENH6;
    applyButton.layer.masksToBounds = YES;
    [applyButton addTarget:self action:@selector(clickApplyButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.applyButton = applyButton;
    
    [self.view addSubview:self.applyButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topImageView).offset(5 * SCREENH6);
        make.top.mas_equalTo(self.topImageView).offset(kStatusBarHeight);
        make.width.mas_equalTo(40 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.centerX.mas_equalTo(self.topImageView);
    }];
    
    [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.bottomBackView.mas_bottom).offset(70 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
}


#pragma mark collectionView代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GroupItemsCollectionViewCell *cell = (GroupItemsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"GroupItemsCollectionViewCell" forIndexPath:indexPath];
    //设置数据
//    cell.model = self.bannerArray[indexPath.row];
    
    return cell;
}


- (UIView *)setupBackView{
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    return backView;
}


- (void)clickApplyButton{
    
    
}

- (CGSize)preferredSizeWithMaxWidth:(CGFloat)maxWidth withLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, 10000000.0f)];
    size.width = fmin(size.width, maxWidth); //在numberOfLine为1模式下返回的可能会比maxWidth大，所以这里我们限制下
    return size;
}

- (void)clickBackButton{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
